package com.irdstudio.biz.rule.api.rest;

import java.util.List;
import java.math.BigDecimal;

import com.irdstudio.biz.rule.common.BizUtil;
import com.irdstudio.sdk.ssm.vo.UserInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.irdstudio.biz.rule.service.facade.SRuleCollDetailService;
import com.irdstudio.biz.rule.service.vo.SRuleCollDetailVO;
import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.beans.web.controller.AbstractController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api")
public class SRuleCollDetailController extends AbstractController  {
	
	@Autowired
	@Qualifier("sRuleCollDetailServiceImpl")
	private SRuleCollDetailService sRuleCollDetailService;

	@Autowired
	protected HttpServletRequest httpRequest;

	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/details", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollDetailVO>> querySRuleCollDetailAll(
			SRuleCollDetailVO vo) {		
		List<SRuleCollDetailVO> outputVo = sRuleCollDetailService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/detail/{dtlId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRuleCollDetailVO> queryByPk(@PathVariable("dtlId") String dtlId) {		
		SRuleCollDetailVO inVo = new SRuleCollDetailVO();
				inVo.setDtlId(new String(dtlId));
		SRuleCollDetailVO outputVo = sRuleCollDetailService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sRuleCollDetail
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/detail", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleCollDetailVO inSRuleCollDetailVo) {		
		try {
			int outputVo = sRuleCollDetailService.deleteByPk(inSRuleCollDetailVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}
	}

	/**
	 * 根据主键删除信息
	 * @param sRuleCollDetail
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/detail/list", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByList(@RequestBody List<SRuleCollDetailVO> list) {
		try {
			int num = 0;
			if (CollectionUtils.isNotEmpty(list)) {
				for (SRuleCollDetailVO vo : list) {
					num += sRuleCollDetailService.deleteByPk(vo);
				}
			}
			return getResponseData(num);
		} catch (Exception e) {
			return getResponseData(-1);
		}

	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRuleCollDetailVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/detail", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleCollDetailVO inSRuleCollDetailVo) {
		try {
			int outputVo = sRuleCollDetailService.updateByPk(inSRuleCollDetailVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}

	}
	
	/**
	 * 新增数据
	 * @param inSRuleCollDetailVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/detail", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleCollDetail(@RequestBody SRuleCollDetailVO inSRuleCollDetailVo) {
		try {
			int outputVo = sRuleCollDetailService.insertSRuleCollDetail(inSRuleCollDetailVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}

	}



	/**
	 * 新增数据
	 * @param inSRuleCollInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/detail/add", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleCollDetailList(@RequestBody List<SRuleCollDetailVO> list) {

		if (CollectionUtils.isEmpty(list)) {
			getResponseData(-1);
		}
		// 回写当前操作员信息
		UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
		for (SRuleCollDetailVO inSRuleCollInfoVo : list) {
			inSRuleCollInfoVo.setCreateUser(userInfo.getUserId());
			inSRuleCollInfoVo.setCreateTime(BizUtil.getCurrentTime());
			inSRuleCollInfoVo.setUpdateUser(userInfo.getUserId());
			inSRuleCollInfoVo.setUpdateTime(BizUtil.getCurrentTime());
		}
		try {
			int outputVo = sRuleCollDetailService.insertSRuleCollDetailList(list);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}
	}
}
